package com.dream.technology.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dream.technology.components.context.FlowContext;
import com.dream.technology.db.dao.RcThirdPartyCommunicationMapper;
import com.dream.technology.db.entity.RcThirdPartyCommunication;
import com.dream.technology.db.entity.RcThirdPartyCommunicationQuery;
import com.dream.technology.db.entity.RcThirdPartyCommunicationWithBLOBs;
import com.dream.technology.service.RcThirdPartyCommunicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 描述：
 *
 * @author chen_q_i@163.com
 * 2018/12/28 : 11:16.
 * @version : 1.0
 */
@Service("rcThirdPartyCommunicationService")
public class RcThirdPartyCommunicationServiceImpl implements RcThirdPartyCommunicationService {

    static  String taskId= "taskId";
    static  String url  = "url";
    static  String respBody= "respBody";
    static  String method= "method";


    @Autowired
    private RcThirdPartyCommunicationMapper communicationMapper;

    @Override
    public int countByExample(RcThirdPartyCommunicationQuery example) {
        return 0;
    }

    @Override
    public int deleteByExample(RcThirdPartyCommunicationQuery example) {
        return 0;
    }

    @Override
    public int deleteByPrimaryKey(Long id) {
        return 0;
    }

    @Override
    public int insert(RcThirdPartyCommunicationWithBLOBs record) {
        return communicationMapper.insert(record);
    }

    @Override
    public int insertSelective(RcThirdPartyCommunicationWithBLOBs record) {
        return communicationMapper.insertSelective(record);
    }

    @Override
    public List<RcThirdPartyCommunicationWithBLOBs> selectByExampleWithBLOBs(RcThirdPartyCommunicationQuery example) {
        return null;
    }

    @Override
    public List<RcThirdPartyCommunication> selectByExample(RcThirdPartyCommunicationQuery example) {
        return null;
    }

    @Override
    public RcThirdPartyCommunicationWithBLOBs selectByPrimaryKey(Long id) {
        return null;
    }

    @Override
    public int updateByExampleSelective(RcThirdPartyCommunicationWithBLOBs record, RcThirdPartyCommunicationQuery example) {
        return 0;
    }

    @Override
    public int updateByExampleWithBLOBs(RcThirdPartyCommunicationWithBLOBs record, RcThirdPartyCommunicationQuery example) {
        return 0;
    }

    @Override
    public int updateByExample(RcThirdPartyCommunication record, RcThirdPartyCommunicationQuery example) {
        return 0;
    }

    @Override
    public int updateByPrimaryKeySelective(RcThirdPartyCommunicationWithBLOBs record) {
        return 0;
    }

    @Override
    public int updateByPrimaryKeyWithBLOBs(RcThirdPartyCommunicationWithBLOBs record) {
        return 0;
    }

    @Override
    public int updateByPrimaryKey(RcThirdPartyCommunication record) {
        return 0;
    }

    /**
     * 保存天机报告的返回
     *
     * @param flowContext
     * @param url
     * @param respBody
     * @return
     */
    @Override
    public int saveTianJiReq(FlowContext flowContext, String url, String respBody) {

        RcThirdPartyCommunicationWithBLOBs thirdPartyCommunication = getRcThirdPartyCommunicationByContext(flowContext);
        thirdPartyCommunication.setTaskId("tianji");
        thirdPartyCommunication.setType(1);


//        String date = DateUtils.formatDate(new Date());
        thirdPartyCommunication.setRequestHeaders("");
        thirdPartyCommunication.setRequestMethod("get");
        thirdPartyCommunication.setRequestTime("date");
        thirdPartyCommunication.setRequestUrl(url);
//        thirdPartyCommunication.setResponseTime(date);
        thirdPartyCommunication.setResponseStatus("200");

        thirdPartyCommunication.setResponseBody(respBody);
        insertSelective(thirdPartyCommunication);
        return 1;

    }

    /**
     * 保存魔蝎的返回
     *
     * @param flowContext
     * @param url
     * @param respBody
     * @return
     */
    @Override
    public int saveMoxieReq(FlowContext flowContext, String url, String respBody) {
        RcThirdPartyCommunicationWithBLOBs thirdPartyCommunication = getRcThirdPartyCommunicationByContext(flowContext);
        thirdPartyCommunication.setTaskId("moxie");
        thirdPartyCommunication.setType(2);
//        String date = DateUtils.formatDate(new Date());
        thirdPartyCommunication.setRequestHeaders("");
        thirdPartyCommunication.setRequestMethod("get");
        thirdPartyCommunication.setRequestTime("date");
        thirdPartyCommunication.setRequestUrl(url);
//        thirdPartyCommunication.setResponseTime(date);
        thirdPartyCommunication.setResponseStatus("200");

        thirdPartyCommunication.setResponseBody(respBody);
        insertSelective(thirdPartyCommunication);
        return 0;
    }

    @Override
    public int saveByJson(FlowContext flowContext, JSONObject jsonObject) {
        RcThirdPartyCommunicationWithBLOBs thirdPartyCommunication = getRcThirdPartyCommunicationByContext(flowContext);
        thirdPartyCommunication.setTaskId(jsonObject.getString(taskId));
        thirdPartyCommunication.setType(2);
//        String date = DateUtils.formatDate(new Date());
        thirdPartyCommunication.setRequestHeaders("");
        thirdPartyCommunication.setRequestMethod(jsonObject.getString(method));
        thirdPartyCommunication.setRequestTime("date");
        thirdPartyCommunication.setRequestUrl(url);
//        thirdPartyCommunication.setResponseTime(date);
        thirdPartyCommunication.setResponseStatus("200");

        thirdPartyCommunication.setResponseBody(respBody);
        insertSelective(thirdPartyCommunication);
        return 0;
    }


    private RcThirdPartyCommunicationWithBLOBs getRcThirdPartyCommunicationByContext(FlowContext flowContext) {
        RcThirdPartyCommunicationWithBLOBs thirdPartyCommunication = new RcThirdPartyCommunicationWithBLOBs();
//        thirdPartyCommunication.setResMap(result);
        thirdPartyCommunication.setComplete(true);
        thirdPartyCommunication.setMobile((String) flowContext.get("customer.mobile"));
        thirdPartyCommunication.setIdNumber((String) flowContext.get("idCard.number"));
        thirdPartyCommunication.setName((String) flowContext.get("customer.realName"));
        thirdPartyCommunication.setBorrowFormId((String) flowContext.get(FlowContext.ORDERFORM_ID));
        return thirdPartyCommunication;
    }


}
